package com.hdaccp.foodsales.controller;

import com.hdaccp.foodsales.dao.GroupsDao;
import com.hdaccp.foodsales.dao.PermissionDao;
import com.hdaccp.foodsales.dao.UserPermissionDao;
import com.hdaccp.foodsales.entity.Groups;
import com.hdaccp.foodsales.entity.Permission;
import com.hdaccp.foodsales.entity.UserPermission;
import com.hdaccp.foodsales.pojo.PermissionBean;
import com.hdaccp.foodsales.pojo.StataBean;
import com.hdaccp.foodsales.service.GroupsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Role;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
@Scope("prototype")
public class GroupsController {
    @Autowired
    private GroupsDao GroupsDao ;
    @Autowired
    private GroupsService GroupsService ;
    @Autowired
    private PermissionDao PermissionDao;
    @Autowired
    private UserPermissionDao UserPermissionDao;
    @RequestMapping("/doFindAll")
    @ResponseBody
    public Map<String,Object> doFindAll(@RequestParam(value = "rname",required = false,defaultValue = "") String rname, int limit,int offset){
        Map<String,Object> map = new HashMap<>() ;
        map.put("total",GroupsService.findCount(rname)) ;
        map.put("rows",GroupsService.findAll(rname,limit,offset)) ;
        return map ;
    }
@RequestMapping("/query")
@ResponseBody
    public List<Groups> query(int userid){
        Groups groups =GroupsDao.getRoleByUserId(userid);
        List<Groups> list =GroupsDao.queryAll();
        for (Groups r:list){
            if (groups.getGid()==r.getGid()){
                r.setIschecked(true);
            }
        }
    return list;
    }


    @RequestMapping("/doFenPen")
    @ResponseBody
    public List<PermissionBean> queryTree(int roleid){
        List<Permission> listAll =UserPermissionDao.query1(roleid);
        List<PermissionBean> list =  PermissionDao.queryByParentId(0) ;
        for(PermissionBean permissionBean : list){
            int id = permissionBean.getpId() ;
            List<PermissionBean> list1 =  PermissionDao.queryByParentId(id) ;
            permissionBean.setNodes(list1);
        }

        for (PermissionBean per:list) {
            StataBean stataBean = new StataBean();
            for (Permission permission : listAll) {
                if (per.getpId() == permission.getpId()) {
                    stataBean.setChecked(true);
                }
            }
            per.setState(stataBean);

            List<PermissionBean> listSon = per.getNodes();
            for (PermissionBean permission1 : listSon) {
                StataBean stateBean1 = new StataBean();
                for (Permission permission11 : listAll) {
                    if (permission1.getpId() == permission11.getpId()) {
                        stataBean.setChecked(true);
                        stateBean1.setChecked(true);
                    }
                }
                permission1.setState(stateBean1);
            }
        }
        return  list ;
    }
}
